package com.lingshi.shopping_manager_api.service.impl;


import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import com.lingshi.shopping.entity.Admin;
import com.lingshi.shopping.enums.ErrorEnums;
import com.lingshi.shopping.exception.BusException;
import com.lingshi.shopping.result.BaseResult;
import com.lingshi.shopping.service.AdminService;
import com.lingshi.shopping_manager_api.service.LoginService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl  implements LoginService {

    @DubboReference
    private AdminService adminService;

    @Override
    public BaseResult login(String username, String password) {

        Admin admin = adminService.findByAdminName(username);

        if (Objects.nonNull(admin)) {
            //对用户输入的密码加密
            String md5 = SaSecureUtil.md5(password);

            //比较密码
            if (Objects.equals(admin.getPassword(), md5)) {
               //登录
                StpUtil.login(username);
                //获取登录的token
                String tokenValue = StpUtil.getTokenValue();
                return BaseResult.success(tokenValue);
            }
        }
//        return BaseResult.error(401,"账号密码错误");
        throw new BusException(ErrorEnums.LOGIN_ERROR);
    }
}
